package neusoft.example.com.yuejian06.Activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import neusoft.example.com.yuejian06.Bean.Clubvideo;
import neusoft.example.com.yuejian06.Model.ClubvideoDetailModel;
import neusoft.example.com.yuejian06.PublicIp;
import neusoft.example.com.yuejian06.R;
import neusoft.example.com.yuejian06.RetrofitListener;

public class ClubvideoDetailActivity extends AppCompatActivity {
    private String clubvideo_id,club_id;
    private ImageView clubvideo_return;
    private TextView clubvideo_name,clubvideo_intro;
    private VideoView clubvideo_video;
    private RetrofitListener<Clubvideo> listener;
    private ClubvideoDetailModel model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clubvideo_detail);

        initView();
        listener();
        initEvents();
        model=new ClubvideoDetailModel();
        model.getClubvideoById(clubvideo_id,listener);

    }

    public void initView(){
        clubvideo_id=getIntent().getStringExtra("clubvideo_id");
        clubvideo_return=findViewById(R.id.clubvideo_return);
        clubvideo_name=findViewById(R.id.clubvideo_name);
        clubvideo_video=findViewById(R.id.clubvideo_video);
        clubvideo_intro=findViewById(R.id.clubvideo_intro);
    }

    public void listener(){
        listener=new RetrofitListener<Clubvideo>() {
            @Override
            public void onResponse(Clubvideo l) {
                clubvideo_name.setText(l.getClubvideo_name());
                clubvideo_intro.setText(l.getClubvideo_intro());
                clubvideo_video.setMediaController(new MediaController(ClubvideoDetailActivity.this));
                clubvideo_video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        Toast.makeText(ClubvideoDetailActivity.this,"播放完成了",Toast.LENGTH_LONG).show();
                    }
                });
                clubvideo_video.setVideoPath(PublicIp.ip+"static/videos/"+l.getClubvideo_video());
                clubvideo_video.requestFocus();
                clubvideo_video.start();
                club_id=l.getClub_id();

            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(ClubvideoDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        };
    }
    public void initEvents(){
        clubvideo_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ClubvideoDetailActivity.this,ClubDetailActivity.class);
                intent.putExtra("club_id",club_id);
                startActivity(intent);
            }
        });
    }
}
